//服务器接口地址
const baseURL : string = 'http://xxxxxx'
//本地调试接口地址
// const baseURL:string='http://xxxxxx'



interface RequestOptions {
	url : string;
	method : 'GET' | 'POST' | 'OPTIONS' | 'HEAD' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT';
	data ?: any;
	header ?: any;
}

// 封装公共请求方法
function request<T>(options : RequestOptions) : Promise<T> {
	return new Promise(function (resolve, reject) {
		let header : any
		if (uni.getStorageSync('token') !== undefined && uni.getStorageSync('token') !== "") {
			header = {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token')
			};
		} else {
			header = {
				'content-type': 'application/json',
			};
		}
		uni.request({
			url: `${baseURL}${options.url}`,
			method: options.method,
			data: options.data,
			header: options.header || header,
			success(res : any) {
				console.log(res)
				uni.hideLoading()
				if (res.data.code == "200") {
					resolve(res.data);
				} else {
					//其他异常
					uni.showToast({
						title: res.data.msg,
						icon: 'error',
						duration:2000
					})
					reject(res.data);
				}
			},
			fail(err) {
				uni.hideLoading()
				//请求失败
				uni.showToast({
					title: '无法连接到服务器',
					icon: 'error',
					duration:2000
				})
				reject(err)
			}
		})
	})
}
export { request, baseURL }